<?php

namespace fztadmin\modules\platform\models\forms;

use common\components\fenzhangtong\FenZhangTong;
use common\Tools;
use wodrow\yii2wtools\tools\ArrayHelper;

class LogPlatformCapitalOperation extends \common\models\db\LogPlatformCapitalOperation
{
    const OUT_ACCT_TYPE_01 = "01";
    const OUT_ACCT_TYPE_02 = "02";
    const OUT_ACCT_TYPE_03 = "03";
    const OUT_ACCT_TYPE_04 = "04";
    const OUT_ACCT_TYPE_05 = "05";

    public function getOutAcctTypeDesc()
    {
        return [
            self::OUT_ACCT_TYPE_01 => "普通资金分户",
            self::OUT_ACCT_TYPE_02 => "交易手续费分户",
            self::OUT_ACCT_TYPE_03 => "提现手续费分户",
            self::OUT_ACCT_TYPE_04 => "营销费用分户",
            self::OUT_ACCT_TYPE_05 => "转账验证子账户",
        ];
    }

    const IN_ACCT_TYPE_01 = "01";
    const IN_ACCT_TYPE_02 = "02";
    const IN_ACCT_TYPE_03 = "03";
    const IN_ACCT_TYPE_04 = "04";
    const IN_ACCT_TYPE_05 = "05";

    public function getInAcctTypeDesc()
    {
        return [
            self::IN_ACCT_TYPE_01 => "普通资金分户",
            self::IN_ACCT_TYPE_02 => "交易手续费分户",
            self::IN_ACCT_TYPE_03 => "提现手续费分户",
            self::IN_ACCT_TYPE_04 => "营销费用分户",
            self::IN_ACCT_TYPE_05 => "转账验证子账户",
        ];
    }

    const ACCT_TYPE_04 = "04";

    public function getAcctTypeDesc()
    {
        return [
            self::ACCT_TYPE_04 => "营销费用分户",
        ];
    }

    const SCENARIO_TYPE_IESI111013 = "IESI111013";
    const SCENARIO_TYPE_IESI111014 = "IESI111014";
    const SCENARIO_TYPE_IESI111015 = "IESI111015";
    const SCENARIO_TYPE_IESI111016 = "IESI111016";

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios = ArrayHelper::merge($scenarios, [
            self::SCENARIO_TYPE_IESI111013 => [],
            self::SCENARIO_TYPE_IESI111014 => [],
            self::SCENARIO_TYPE_IESI111015 => [],
            self::SCENARIO_TYPE_IESI111016 => [],
        ]);
        return $scenarios;
    }

    public function rules()
    {
        $rules = parent::rules();
        $rules = ArrayHelper::merge($rules, [
            [['out_acct_type', 'in_acct_type'], 'required', 'on' => self::SCENARIO_TYPE_IESI111015],
            [['acct_type', 'plat_cust_no'], 'required', 'on' => self::SCENARIO_TYPE_IESI111016],
        ]);
        return $rules;
    }

    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels = ArrayHelper::merge($attributeLabels, [
            'out_acct_type' => "转出分户类型",
            'in_acct_type' => "转入账户类型",
            'acct_type' => "转出分户类型",
        ]);
        return $attributeLabels;
    }

    public function cz(FenZhangTong &$fzt)
    {
        $type = $this->type;
        $params = [
            'platNo' => $this->plat_no,
            'transId' => $this->trans_id,
            'transAmt' => $this->trans_amount,
        ];
        $fzt->$type($params);
        $this->_log($fzt);
        switch ($fzt->ret_code){
            case "00000":
                return true;
                break;
            default:
                \Yii::$app->session->addFlash("error", Tools::toJson($fzt->resp_arr));
                return false;
                break;
        }
    }

    public function tx(FenZhangTong &$fzt)
    {
        $type = $this->type;
        $params = [
            'platNo' => $this->plat_no,
            'transId' => $this->trans_id,
            'transAmt' => $this->trans_amount,
        ];
        $fzt->$type($params);
        $this->_log($fzt);
        switch ($fzt->ret_code){
            case "00000":
                return true;
                break;
            default:
                \Yii::$app->session->addFlash("error", Tools::toJson($fzt->resp_arr));
                return false;
                break;
        }
    }

    public function transferOfSelfOwnedFundsBetweenHouseholds(FenZhangTong &$fzt)
    {
        $type = $this->type;
        $params = [
            'platNo' => $this->plat_no,
            'transId' => $this->trans_id,
            'transAmt' => $this->trans_amount,
            'outAcctType' => $this->out_acct_type,
            'inAcctType' => $this->in_acct_type,
        ];
        $fzt->$type($params);
        $this->_log($fzt);
        switch ($fzt->ret_code){
            case "00000":
                return true;
                break;
            default:
                \Yii::$app->session->addFlash("error", Tools::toJson($fzt->resp_arr));
                return false;
                break;
        }
    }

    public function memberAccountsTransfer(FenZhangTong &$fzt)
    {
        $type = $this->type;
        $params = [
            'platNo' => $this->plat_no,
            'transId' => $this->trans_id,
            'transAmt' => $this->trans_amount,
            'acctType' => $this->acct_type,
            'platCustNo' => $this->plat_cust_no,
        ];
        $fzt->$type($params);
        $this->_log($fzt);
        switch ($fzt->ret_code){
            case "00000":
                return true;
                break;
            default:
                \Yii::$app->session->addFlash("error", Tools::toJson($fzt->resp_arr));
                return false;
                break;
        }
    }

    protected function _log(FenZhangTong &$fzt)
    {
        $this->resp_cont = Tools::toJson($fzt->resp_arr);
        $this->ret_code = $fzt->ret_code;
        $this->ret_msg = $fzt->ret_msg;
        $this->save();
    }
}